package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.MerchantModel;
import com.xieyingwangluo.yxpart.service.MerchantService;
import com.xieyingwangluo.yxpart.utils.StringBuildUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MerchantLogic {
    @Autowired
    private MerchantService merchantService;

    //商户登录
    public Result checklogin(HttpSession session,
                             String tel,
                             String pwd
    ) {
        Map<String, Object> map = new HashMap<>();
        MerchantModel merchantModel = merchantService.check(tel, pwd);
        if (merchantModel != null) {
            merchantModel.setPwd("");
            session.setAttribute("merchantModel", merchantModel);
            map.put("Login", "商户");
            map.put("User", merchantModel);
            return Result.generate(0, "Login success", map);
        }
        return Result.generate(1, "Login lose", null);
    }

    //商户退出登录
    public Result outlogin(
            HttpSession session
    ) {
        session.getAttribute("merchantModel");
        session.invalidate();
        return Result.generate(0, "Out success", null);
    }

    //商户注册添加
    public Result add(
            MerchantModel merchantModel
    ) {
        merchantModel.setNoid(StringBuildUtils.buildUUID());
        merchantModel.setState(1);
        merchantModel.setVip(1);
        merchantModel.setHead_img("/src/main/resources/img/head.jpg");
        merchantModel.setIntg(600);
        if (merchantModel.getTel() != null) {
            MerchantModel merchantModel1 = new MerchantModel();
            merchantModel1.setTel(merchantModel.getTel());
            List<MerchantModel> list = merchantService.selA(merchantModel1);
            if (list.size() > 0) {
                return Result.generate(1, "User already exists", null);
            }
        }
        int cnt = merchantService.add(merchantModel);
        if (cnt > 0) {
            return Result.generate(0, "Add success", cnt);
        }
        return Result.generate(1, "Add fail", null);
    }

    //修改商户信息
    public Result upd(MerchantModel merchantModel) {
        MerchantModel merchant = merchantService.selO(merchantModel.getNoid());

        if (merchant.getTel() != null || merchant.getTel().equals(merchantModel.getTel())) {
            int cnt = merchantService.update(merchantModel);
            return Result.generate(0, "Upd success", cnt);
        } else {
            MerchantModel merchantModel1 = new MerchantModel();
            merchantModel1.setTel(merchantModel.getTel());
            List<MerchantModel> list = merchantService.selA(merchantModel1);

            if (list.size() == 0) {
                int cnt = merchantService.update(merchantModel);
                return Result.generate(0, "Upd success", cnt);
            }
        }

        return Result.generate(1, "Upd fail", null);
    }

    //单个商户信息展示
    public Result selO(
            @RequestParam("noid") String noid
    ) {
        MerchantModel merchantModel = merchantService.selO(noid);
        merchantModel.setPwd("");
        return Result.generate(0, "Select success", merchantModel);
    }

    //商户信息列表展示
    public Result selA(MerchantModel merchantModel){
        List<MerchantModel> list =  merchantService.selA(merchantModel);
        return Result.generate(0, "Select success", list);
    }
}
